﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Text;

namespace TraditionalMalteseCuisine.App_Code.Objects
{
    [Serializable]
    public class RecipeComment : BaseTable
    {
        //*************************************************
        //                Class Members
        //*************************************************
        #region Class Members
        public const string TABLE_NAME = "RecipesComments";
        public const string VIEW_NAME_ALL_RECIPES_COMMENTS = "vw" + "All" + TABLE_NAME;

        public const string RECIPE_ID_TAG = "recipe_" + Recipe.ID_TAG;
        public const string MEMBER_ID_TAG = "member_" + SiteMember.ID_TAG;
        public const string COMMENT_TAG = "comment";
        public const string POSTED_DATE_TAG = "posted_date";

        private int mRecipeID = -1;
        private int mMemberID = -1;
        private string mComment = string.Empty;
        private DateTime mPostedDate = DateTime.MinValue;
        #endregion
        //*************************************************


        //*************************************************
        //                 Constructor
        //*************************************************
        #region Constructor
        public RecipeComment()
        {
        
        }
        #endregion
        //*************************************************


        //*************************************************
        //                   Methods
        //*************************************************
        #region Methods
        protected override string GetInsertScriptUsingParameters()
        {
            StringBuilder insertScript = new StringBuilder();

            insertScript.Append("INSERT INTO ");
            insertScript.AppendFormat("{0} ", TABLE_NAME);
            insertScript.AppendFormat("([{0}], [{1}], [{2}]) ", RECIPE_ID_TAG, MEMBER_ID_TAG, COMMENT_TAG);
            insertScript.Append("VALUES ");
            insertScript.AppendFormat("(@{0}, @{1}, @{2}) ", RECIPE_ID_TAG, MEMBER_ID_TAG, COMMENT_TAG);

            return insertScript.ToString();
        }

        public override OleDbCommand GetInsertCommand()
        {
            OleDbCommand insertCommand = new OleDbCommand();

            insertCommand.CommandType = CommandType.Text;
            insertCommand.CommandText = GetInsertScriptUsingParameters();

            insertCommand.Parameters.AddWithValue(string.Format("@{0}", RECIPE_ID_TAG), ((RecipeID > -1) ? (object)RecipeID : DBNull.Value));
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", MEMBER_ID_TAG), ((MemberID > -1) ? (object)MemberID : DBNull.Value));
            insertCommand.Parameters.AddWithValue(string.Format("@{0}", COMMENT_TAG), Comment);

            return insertCommand;
        }

        public override BaseTable ReadDataRow(DataRow datarow)
        {
            ID = int.Parse(datarow[ID_TAG].ToString());
            RecipeID = int.Parse(datarow[RECIPE_ID_TAG].ToString());
            MemberID = int.Parse(datarow[MEMBER_ID_TAG].ToString());
            Comment = datarow[COMMENT_TAG].ToString();
            PostedDate = DateTime.Parse(datarow[POSTED_DATE_TAG].ToString());
            InUse = bool.Parse(datarow[IN_USE_TAG].ToString());

            return this;
        }


        public override string ToString()
        {
            return string.Format("{0}", Comment);
        }
        #endregion
        //*************************************************


        //*************************************************
        //                  Properties
        //*************************************************
        #region Properties
        public int RecipeID
        {
            get { return mRecipeID; }
            set { mRecipeID = value; }
        }

        public int MemberID
        {
            get { return mMemberID; }
            set { mMemberID = value; }
        }

        public string Comment
        {
            get { return mComment; }
            set { mComment = value; }
        }

        public DateTime PostedDate
        {
            get { return mPostedDate; }
            set { mPostedDate = value; }
        }
        #endregion
        //*************************************************
    }
}